#!/usr/bin/env python3
import shutil
import os
import getpass

home_path = os.path.expandvars('$HOME')
config_path = os.path.join(home_path, '.config/aria2')

def install_ariang():

    cur_path = os.path.abspath(os.curdir)

    if os.path.exists(config_path) :
        shutil.rmtree(config_path)

    shutil.copytree('./config/aria2', config_path)


    os.chdir(config_path)
    # 把aria2.conf配置文件中的test用户改为当前用户
    cur_usr = getpass.getuser()
    cmd_str = "sed -i s/test/" + cur_usr + "/g aria2.conf"
    os.system(cmd_str)
    os.system("sudo chmod +x  delete.sh clean.sh")

    os.chdir(cur_path)
    os.system("sudo cp -f ./aria.service /lib/systemd/system/aria.service")
    cmd_str = "sed -i s/test/" + cur_usr + "/g aria.service"
    os.system(cmd_str)

    os.system("sudo systemctl daemon-reload")
    os.system("sudo systemctl start aria")
    os.system("sudo systemctl enable aria")
    
    os.system("sudo mkdir -p /var/www/html/ariang")
    # 有墙可能下不下来
    # os.system("wget https://github.com//mayswind/AriaNg/releases/download/1.2.1/AriaNg-1.2.1.zip")
    # 这是用firefox的插件下载下来后，复制的下载地址，原来是多了个代理。既然已经下好了，就直接把压缩包放进来了，减少安装时间。
    # os.system("wget https://github.91chifun.workers.dev/https://github.com//mayswind/AriaNg/releases/download/1.2.1/AriaNg-1.2.1.zip")
    os.system("sudo cp -f ./AriaNg-1.2.2.zip /var/www/html/ariang")
    os.chdir("/var/www/html/ariang")
    os.system("sudo unzip -f AriaNg-1.2.2.zip")

if __name__=="__main__":
    install_ariang()

